class Java {
  /**
    * 运行时间: 2 ms
    * 内存消耗: 39.4 MB
   */
  public static int pivotIndex(int[] nums) {
    if (nums == null) return -1;
    if (nums.length == 1) return 0;
    if (nums.length == 3) {
      if (nums[0] == nums[2]) return 1;
      else return -1;
    }
    int sum = 0;
    for (int i = 0; i < nums.length; i++) {
      sum += nums[i];
    }
    int leftTotal = 0;
    for (int i = 0; i < nums.length; i++) {
      sum -= nums[i];
      if (leftTotal == sum)
        return i;
      leftTotal += nums[i];
    }
    return -1;
  }

  public static void main(String[] args) {
    // int nums[] = {0};
    // int nums[] = {1,1};
    // int nums[] = {1,2,1};
    // int nums[] = {1,2,1,2};
    int nums[] = {1,7,3,6,5,6};

    // System.out.println(pivotIndex(nums));

    // 异常情况
    // System.out.println(pivotIndex(null));
  }
}